<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>添加角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/oauthplatform/static/css/x-admin/font.css">
    <link rel="stylesheet" href="/oauthplatform/static/css/x-admin/xadmin.css">
    <link rel="stylesheet" href="/oauthplatform/static/lib/zTree/css/zTreeStyle/zTreeStyle.css">
    <style>
        .layui-textarea { display: block; width: 70%; padding-left: 10px; }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" id="dataForm">

            <div class="layui-form-item">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" required  lay-verify="required" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">必填</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">角色代码</label>
                <div class="layui-input-inline">
                    <input type="text" name="code" required lay-verify="required" placeholder="请输入角色代码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">必填</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-inline">
                    <select name="isValid" lay-verify="required">
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">授权</label>
                <div class="layui-input-inline">
                    <div class="layui-card-body">
                        <div id="treeDiv" class="ztree"></div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label for="desc" class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" name="remarks" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
            <input type="hidden" value="add" name="tag">
        </form>
    </div>
</div>
<script type="text/javascript" src="/oauthplatform/static/lib/layui/layui.all.js" charset="utf-8"></script>
<script type="text/javascript" src="/oauthplatform/static/lib/jquery/jquery.3.4.1.js"></script>
<script type="text/javascript" src="/oauthplatform/static/lib/x-admin/xadmin.js"></script>
<script src="/oauthplatform/static/lib/other/html5.min.js"></script>
<script src="/oauthplatform/static/lib/other/respond.min.js"></script>
<script src="/oauthplatform/static/lib/zTree/js/jquery.ztree.all.js"></script>
<script type="application/javascript">
    var treeObj;
    $(function(){ loadTree(); })
    function loadTree(){
        var setting = {
            async:{
                enable : true,
                url : '/oauthplatform/authorization/tree',
                type : "post",
                otherParam:{rid:$("#rid").val()}
            },
            data:{ simpleData:{ enable : true, idKey : "id", pIdKey : "pid" } },
            check:{
                enable:true,
                chkStyle:"checkbox",
                chkboxType : { "Y" : "", "N" : "" }
            }
        };
        treeObj = $.fn.zTree.init($("#treeDiv"), setting);
    }


    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            $.ajax({
                type: "POST",//方法类型
                url: "/oauthplatform/role/save" ,//url
                data: $('#dataForm').serialize(),
                success: function (result) {
                    layer.msg(result.msg);
                    //提交权限菜单
                    if(result.msg == "操作成功"){
                        var nodes = treeObj.getCheckedNodes(true);
                        var ids = "";
                        if(nodes != null && nodes.length > 0){
                            for(var i = 0;i<nodes.length;i++){
                                var node = nodes[i];
                                ids += node.id + ",";
                            }
                        }

                        $.ajax({
                            type: "POST",//方法类型
                            url: "/oauthplatform/authorization/save" ,//url
                            data: {rid:result.code,pids:ids},
                            success: function (result) {
                                layer.msg(result.msg);
                                if(parseInt(result.code) === 200){
                                    setTimeout(function(){
                                        xadmin.close();
                                        xadmin.father_reload();
                                    }, 900);
                                }
                            },
                            error: function (result) {
                                layer.msg("系统异常,请联系管理员");
                            }
                        });
                    }
                },
                error: function (result) {
                    layer.msg("系统异常,请联系管理员");
                }
            });
            return false;
        });
    });
</script>
</html>